package com.codeos.khttp.request.body;

import com.codeos.khttp.utils.KHttpUtils;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

public class KFormBody implements KHttpBody {

//    private StringBuffer mHttpHeader = new StringBuffer();

    private List<KBodyItem> mBodyItemList = new LinkedList<KBodyItem>();
    private String mBoundary;
    private String mHeaderBoundary;

    public KFormBody() {
        String strUUID = UUID.randomUUID().toString();
        int lastDashIndex = strUUID.lastIndexOf("-");
//        mBoundary = "---------------------------"+strUUID.substring(lastDashIndex);
        mBoundary="----------------------------7daf10c20d06";
        mHeaderBoundary = "--------------------------7daf10c20d06";
    }

    /**
     * 增加表单文件
     * @param name
     * @param file
     * @param properties
     * @return
     */
    public KFormBody addFile(String name, File file, String properties) {

        mBodyItemList.add(new KBodyItemFile(name, file, properties));

        return this;
    }

    /**
     * 增加纯文本信息
     * @param text
     * @param properties
     * @return
     */
    public KFormBody addText(String name, String text, String properties) {

        mBodyItemList.add(new KBodyItemText(name, text, properties));

        return this;
    }

    @Override
    public boolean isBodyEmpty() {
        return mBodyItemList.size() == 0;
    }

    @Override
    public String getBoundary() {
        return mBoundary;
    }

    @Override
    public String getHeaderBoundary() {
        return mHeaderBoundary;
    }

    @Override
    public int getBodyLength() {
        int length = 0;

        for(KBodyItem item : mBodyItemList) {
            length += item.getItemLength();
        }

        System.out.println("item len: "+length);
        length += (mBodyItemList.size()+1)*(mBoundary.length()+2)+2;
        System.out.println("other len: "+length);

        return length;
    }

    @Override
    public void sendBodyData(OutputStream os, OutputStreamWriter writer) throws IOException {

        for(int i = 0; i < mBodyItemList.size(); i++) {
            KBodyItem item = mBodyItemList.get(i);

            writer.write(mBoundary+ KHttpUtils.gHttpEnd);
            writer.flush();
            item.sendItemData(os, writer);
        }
        writer.write(mBoundary+"--"+ KHttpUtils.gHttpEnd);
        writer.flush();
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();

        for(int i = 0; i < mBodyItemList.size(); i++) {
            sb.append(mBoundary+KHttpUtils.gHttpEnd);
            sb.append(mBodyItemList.get(i).toString());
        }
        sb.append(mBoundary+"--"+KHttpUtils.gHttpEnd);

        return sb.toString();
    }

    public static void main(String[] args) {
//        File file = new File("C:\\Users\\Administrator\\Desktop\\rr.jpg");
//        System.out.println(file.getName());
//        System.out.println(file.getPath());

        for(int i = 0; i < 10; i++) {
            System.out.println(UUID.randomUUID());
        }
    }
}
